﻿using StarPizza.Model.BaseModel;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace StarPizza.ViewModel
{
    class ConfigVM : VisibleVM
    {
        public event Action<string> ConfigDone;

        private string connectionString;
        public string ConnectionString
        {
            get { return connectionString; }
            set
            {
                if (value != connectionString)
                {
                    connectionString = value;
                    OnPropertyChanged("ConnectionString");
                }
            }
        }

        private string selecteConnectionString;
        public string SelectedConnectionString
        {
            get { return selecteConnectionString; }
            set
            {
                if (value != selecteConnectionString)
                {
                    selecteConnectionString = value;
                    ConnectionString = value;
                    OnPropertyChanged("SelectedConnectionString");
                }
            }
        }
        public List<string> ConnectionStrings { get; set; }
        
        private void CollectConnectionStrings()
        {
            ConnectionStringSettingsCollection settings =
                ConfigurationManager.ConnectionStrings;
            if (settings != null)
            {
                foreach (ConnectionStringSettings cs in settings)
                {
                    ConnectionStrings.Add(cs.ConnectionString);
                }
                ConnectionString = settings
                    ["StarPizza.Properties.Settings.StarDatabaseConnectionString"].ConnectionString;
                SelectedConnectionString = ConnectionString;
            }
        }
        public ConfigVM()
        {
            TestResult = "";
            ConnectionStrings = new List<string>();
            CollectConnectionStrings();
        }
        private string testResult;
        public string TestResult
        {
            get { return testResult; }
            set
            {
                if (value != testResult)
                {
                    testResult = value;
                    OnPropertyChanged("TestResult");
                }
            }
        }
        
        private void AppendToTestResult(string line)
        {
            TestResult += line + System.Environment.NewLine;
        }
        void ConnectionTester()
        {
            AppendToTestResult("[" + ConnectionString + "] is beeing tested...");
            
            string result = "[" + ConnectionString + "]";
            try
            {
                using (SqlConnection connection = new SqlConnection(ConnectionString))
                {
                    connection.Open();
                    var dataContext = new StarDataDataContext(connection);
                }
                result += " OK!";
            }
            catch (Exception ex)
            {
                result += " FAILED!";
                result += System.Environment.NewLine;
                result += ex.Message;
            }
            result += System.Environment.NewLine;
            AppendToTestResult(result);
        }
        public void TestConnection()
        {
            new Task(ConnectionTester).Start();
        }
        public void Setup()
        {
            IsVisible = false;
            if (ConfigDone != null)
                ConfigDone(ConnectionString);
        }
    }
}
